#include <QApplication>
#include <QVTKOpenGLNativeWidget.h>
#include <vtkSmartPointer.h>
#include <vtkConeSource.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <QSurfaceFormat>

#include <vtkProperty.h>

#include <vtkAutoInit.h>


void drawCone( QVTKOpenGLNativeWidget& widget){

    // 创建一个圆锥体几何体源
    vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
    cone->SetResolution(32);

    // 创建一个数据映射器，将几何体数据映射到渲染管线
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(cone->GetOutputPort());

    // 创建一个Actor，用于表示场景中的对象
    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();

    cylinder->SetResolution(16);

    vtkSmartPointer<vtkPolyDataMapper> cylinerMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinerMapper->SetInputConnection(cylinder->GetOutputPort());

    vtkSmartPointer<vtkActor> cylineActor = vtkSmartPointer<vtkActor>::New();
    cylineActor->SetMapper(cylinerMapper);
    vtkSmartPointer<vtkProperty> prop = vtkSmartPointer<vtkProperty>::New();
    prop->SetColor(1, 0, 0);
    cylineActor->SetProperty(prop);

    // 创建一个渲染器，负责渲染场景
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->AddActor(cylineActor);
    renderer->SetBackground(0.2, 0.3, 0.4); // 设置背景颜色

    // 将渲染器添加到渲染窗口小部件
    widget.renderWindow()->AddRenderer(renderer);

    // 显示渲染窗口小部件
    widget.show();
}

int main(int argc, char* argv[])
{

    QApplication app(argc, argv);
    // 初始化Qt和VTK的OpenGL上下文配置
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    // 创建一个VTK-Qt集成的渲染窗口小部件
    QVTKOpenGLNativeWidget widget;
    drawCone(widget);
    // 运行Qt应用程序事件循环
    return app.exec();
}
